<?php
class KolSys_Bot
{
	public $pwdhash;
	public static function getInstance()
	{
		return KolSys_Singleton::getInstance(__CLASS__);
	}

	public function init()
	{	
		$mysql=KolSys_MySQLDatabase::connect();
		KolSys_Curl_Header::setCookie(KolSys_MySQLDatabase::getDBCookie());
	}

	public function logOut()
	{
		$auth=KolSys_Auth::getInstance();
		$auth->logOut();
	}

	public function logIn()
	{
		$auth=KolSys_Auth::getInstance();
		$this->pwdhash=$auth->get_pwdhash();
		if(!isset($this->pwdhash) || $this->pwdhash==''|| $this->pwdhash==0 || !$this->pwdhash || $this->pwdhash=='deleted')
		{
			//'<pre>'.print_r($this->pwdhash,true).'</pre>';
			$auth->logOut();
			$auth->logIn();
			$this->pwdhash=$auth->get_pwdhash();
		}

	}

	public function logChatMessage($text)
	{
		global $settings;
		$username=mysql_real_escape_string($settings['kol']['username']);
		$text=mysql_real_escape_string($text);
		$query="INSERT INTO `chat logs`(`id`,`username`,`text`,`timestamp`) VALUES(NULL,'%s','%s',CURRENT_TIMESTAMP)";
		$sql=sprintf($query,$username,$text);
		$query=mysql_query($sql) or die(mysql_error());
	}

	public function setIntoRolloverMode()
	{
		KolSys_Output::getInstance()->output(ROLLOVER_MSG);
		$query=mysql_query("UPDATE `status` SET rollover=1;") or die(mysql_error());
		KolSys_Output::getInstance()->output(SHUTDOWN_MSG);
		exit;
	}
	function countMeat()
	{
		$meatOnMe=false;
		$response=KolSys_Nav::getPage('charpane.php');
		if(ereg("meat\"\);\'><br><span class=black>([^\<]*)</span>",$response,$matches)){
			$meatOnMe=str_replace(",","",$matches[1]);
		}else if(ereg('alt="Meat"><br><span class=black>([^<]*)<\/span>',$response,$matches)){
			$meatOnMe=str_replace(",","",$matches[1]);
		}
		
	return $meatOnMe;	
	}

}//class